package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.ConvertUtils;

import java.util.Arrays;

import android_serialport_api.SerialPortFinder;
import tp.xmaihh.serialport.SerialHelper;
import tp.xmaihh.serialport.bean.ComBean;


public class MainActivity extends AppCompatActivity {
    private TextView sgsj, wdsj;
    private static final String TAG = "test485";
    private SerialPortFinder serialPortFinder;
    private SerialHelper serialHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        sgsj = findViewById(R.id.sgsj);
        wdsj = findViewById(R.id.wdsj);
        sgsj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (serialHelper.isOpen()) {
                        //测试查询湿度
                        serialHelper.sendHex("01060067019039E9");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        wdsj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (serialHelper.isOpen()) {
                        //测试查询湿度
                        serialHelper.sendHex("01030067000135D5");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        serialPortFinder = new SerialPortFinder();
        serialHelper = new SerialHelper("dev/ttyS4", 19200) {
            @Override
            protected void onDataReceived(final ComBean comBean) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "收到数据" + comBean.bRec);
                    }
                });
            }
        };
        try {
            serialHelper.open();
        } catch (Exception e) {
            Log.d(TAG, "串口打开失败: " + e);
        }
        Log.d(TAG, "onCreate: " + Arrays.toString(serialPortFinder.getAllDevices()));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        serialHelper.close();
    }


    public void jumpToshezhi1(View view) {
        Intent intent = new Intent(MainActivity.this, shezhi1.class);
        startActivity(intent);
    }

    public void jumpTosbgl(View view) {
        Intent intent = new Intent(MainActivity.this, sbgl.class);
        startActivity(intent);
    }

    public void jumpTosbgl1(View view) {
        Intent intent = new Intent(MainActivity.this, sbgl1.class);
        startActivity(intent);
    }

    public void jumpTosbgl2(View view) {
        Intent intent = new Intent(MainActivity.this, sbgl2.class);
        startActivity(intent);
    }

    public void jumpTosbgl3(View view) {
        Intent intent = new Intent(MainActivity.this, sbgl3.class);
        startActivity(intent);
    }

    public void jumpTosbgl4(View view) {
        Intent intent = new Intent(MainActivity.this, sbgl4.class);
        startActivity(intent);
    }


}